layui.define(['layer', 'form', 'laytpl'], function(exports){
  var $ = layui.$,
      layer = layui.layer,
      laytpl = layui.laytpl;

  var userConf = {
    key_id: '',
    user_id: '0',
    secret: ''
  }
  var personal = {
    init: function () {

    },
    loadSecret: function () {
      $.ajaxSettings.async = false;
      $.getJSON('/copyright/getUserIdAndSecret', function (result) {
        userConf.key_id = result.keyId;
        userConf.user_id = result.userId;
        userConf.secret = result.secret;
      })
    },
    loadAccountData: function () {
      $.post('/get_user_sale_info',userConf,function (result) {
        console.log(result)
        if(result.result=='success'){
          var balance = result.balance==null?'0.0':result.balance,
              balance_pre = balance.substr(0,balance.indexOf('.')),
              balance_suf = balance.substr(balance.indexOf('.'),2);
          $('.balance').html(balance_pre+'<small>'+balance_suf+'</small>').show();
          var total_amount = result.total_amount==null?'0.0':result.total_amount,
              total_amount_pre = total_amount.substr(0, total_amount.indexOf('.')),
              total_amount_suf = total_amount.substr(total_amount.indexOf('.'),2);
          $('.total_amount').html(total_amount_pre+'<small>'+total_amount_suf+'</small>').show();
          var total_on_sale = result.total_on_sale==null?0:result.total_on_sale;
          $('.total_on_sale').html(total_on_sale).show();
        }
      },'json');
      
      $.get('/copyright/getCommitedWorksNum',function (result) {
        $('.commited').html(result).show();
      })

      $.getJSON('/copyright/findLatestWorks',function (result) {
        var getTpl = $('#latest_works_body').html(),
            tbody = $(".latest_works");
        laytpl(getTpl).render(result, function (html) {
          tbody.html(html);
        });
      })

      $.getJSON('/copyright/findLatestMessages',function (result) {
        var getTpl = $('#latest_message_content').html(),
            tbody = $(".latest_messages");
        laytpl(getTpl).render(result, function (html) {
          tbody.html(html);
        });
      })
    }
  }
  personal.init();
  personal.loadSecret();
  personal.loadAccountData();
  exports('personal', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});